package com.scau.action.business;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ModelDriven;
import com.scau.action.BaseAction;
import com.scau.exception.BusinessException;
import com.scau.model.business.Attribute;
import com.scau.model.business.Category;
import com.scau.model.business.Material;
import com.scau.model.business.MaterialType;
import com.scau.model.business.Rule;
import com.scau.service.impl.business.AttributeService;
import com.scau.service.impl.business.CategoryService;
import com.scau.service.impl.business.MaterialService;
import com.scau.service.impl.business.MaterialTypeService;
import com.scau.service.impl.business.RuleService;
import com.scau.util.PageController;
@Component
@Scope("prototype")
public class MaterialAction extends BaseAction implements Serializable,ModelDriven<Material> {
	private final static Log logger = LogFactory.getLog(MaterialAction.class);
	private AttributeService attributeService;
	private CategoryService categoryService;
	private Material material = null;

	private MaterialService materialService;

	private MaterialTypeService materialTypeService;

	private List<Material> mList = null;

	private PageController pager = null;

	private RuleService ruleService;

	private List<MaterialType> typeList = null;

	/**
	 * 删除
	 * 
	 * @return
	 */
	public String del() {
		return "list";
	}

	/**
	 * 修改
	 * 
	 * @return
	 */
	public String get() {
		material = materialService.get(material);
		typeList = materialTypeService.listAll(new MaterialType());
		List<Rule> ruleList = ruleService.list(new Rule(), null, null,
				new String[] { "status" }, new String[] { "1" });
		List<List<Category>> CList = new ArrayList<List<Category>>();
		List<List<?>> AList = new ArrayList<List<?>>();
		for (int i = 0; i < ruleList.size(); i++) {
			List<Category> categoryList = (new CategoryService()).list(
					new Category(), null, null, new String[] { "rule_id" },
					new Object[] { ruleList.get(i).getId()});
			List<List<Attribute>> listAtt = new ArrayList<List<Attribute>>();
			for (int j = 0; j < categoryList.size(); j++) {
				List<Attribute> attributeList = (new AttributeService())
						.list(new Attribute(), null, null,
								new String[] { "category_id" },
								new Object[] { categoryList.get(j).getId()
										});
				listAtt.add(attributeList);
			}
			CList.add(categoryList);
			AList.add(listAtt);
		}
		request.setAttribute("cList", CList);
		request.setAttribute("aList", AList);
		request.setAttribute("typeList", typeList);
		request.setAttribute("ruleList", ruleList);
		return "edit";
	}

	public AttributeService getAttributeService() {
		return attributeService;
	}

	public CategoryService getCategoryService() {
		return categoryService;
	}

	public Material getMaterial() {
		return material;
	}

	public MaterialService getMaterialService() {
		return materialService;
	}

	public MaterialTypeService getMaterialTypeService() {
		return materialTypeService;
	}

	public List<Material> getMList() {
		return mList;
	}

	@Override
	public Material getModel() {
		// TODO Auto-generated method stub
		return material;
	}

	public PageController getPager() {
		return pager;
	}

	public RuleService getRuleService() {
		return ruleService;
	}

	public List<MaterialType> getTypeList() {
		return typeList;
	}

	/**
	 * 列表显示
	 * 
	 * @return
	 */
	public String list() {
		int totalRows = materialService.getRecordCount(new Material());
		String URL = getListURL();
		this.pager.setURL(URL);
		this.pager.setTotalRowsAmount(totalRows);
		List<Material> resourceList = materialService.list(new Material(),
				this.pager.getPageStartRow(), pager.getPageSize(), null, null);
		typeList = materialTypeService.listAll(new MaterialType());
		request.setAttribute("typeList", typeList);
		pager.setData(resourceList);
		return "list";
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			materialService.save(material);
			return "list";
		} catch (BusinessException e) {
			// 保存原来表单已输入的内容
			request.setAttribute("material", material);
			request.setAttribute("message", e.getMessage());
			return "edit";
		}
	}

	@Resource
	public void setAttributeService(AttributeService attributeService) {
		this.attributeService = attributeService;
	}

	@Resource
	public void setCategoryService(CategoryService categoryService) {
		this.categoryService = categoryService;
	}

	public void setMaterial(Material material) {
		this.material = material;
	}

	@Resource
	public void setMaterialService(MaterialService materialService) {
		this.materialService = materialService;
	}

	@Resource
	public void setMaterialTypeService(MaterialTypeService materialTypeService) {
		this.materialTypeService = materialTypeService;
	}

	public void setMList(List<Material> list) {
		mList = list;
	}

	@Resource
	public void setPager(PageController pager) {
		this.pager = pager;
	}

	@Resource
	public void setRuleService(RuleService ruleService) {
		this.ruleService = ruleService;
	}

	public void setTypeList(List<MaterialType> typeList) {
		this.typeList = typeList;
	}

}
